import { createSlice } from '@reduxjs/toolkit'
import pitStatic from '../../img/2.jpg'

export const UserSlice = createSlice({
    // 命名空间，在调用action的时候会默认的设置为action的前缀,保证唯一.不重名
    name: 'user',
    initialState: {
        user:{
            imgurl: pitStatic,
            AutoCode: '',
            tel: '18317974704',
            name: 'xiaowang',
            sex: '男',
            city:'未知',
            // img: [{url: pitStatic}],
        }
    },
    reducers: {
        // 定义的action-赋值的方式进行数据的改变
        userChange: (state, action) => {
            state.user = { ...state.user, ...action.payload, }
            if (state.user.imgurl == null){
                // console.log('state.user', state.user);
                state.user = { ...state.user, imgurl: pitStatic }
            }
        },
    },
})

// 导出action
export const { userChange } = UserSlice.actions

// 异步数据处理

// 更新用户信息
export const userChangeAsync = (amount) => (dispatch) => {
    setTimeout(() => {
        dispatch(userChange(amount))
    }, 0)
}

// export const selectCount = (state) => state.counter.value

// 导出reducer
const UserSliceReducer = UserSlice.reducer
export default UserSliceReducer